<?php
require_once('Extension/Controller/Plugin/Abstract.php');

class Extension_Controller_Plugin_Common_Console extends Extension_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        self::initConsole();
    }

    //public function routeShutdown(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {}
    //public function preDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function postDispatch(Zend_Controller_Request_Abstract $request) {}
    //public function dispatchLoopShutdown() {}


    /**
     * Console for CLI
     * Clean all cache : 
     * php index.php -m cron -c cache -a clear -A
     * Clean old cache
     * php index.php -m cron -c cache -a clear
     */
    public static function initConsole()
    {
        if (isset($_SERVER['argv']) and !empty($_SERVER['argv'])) {
            $rules = array();
            $rules['m|module-s']        = 'The option module to use.';
            $rules['c|controller=s']    = 'The controller to use.';
            $rules['a|action=s']        = 'The action to use.';
            $rules['A']                 = 'If set, the entire cache will be destroyed.';
            $rules['v|verbose']         = 'If set, Exceptions and other debugging information will be displayed.';

            //$config = self::initConfig();
            $getopt = new Zend_Console_Getopt($rules);
            $getopt->parse();

            $module = $getopt->getOption('module');
            if ('cron' != $module) {
                exit('invalid module !');
            }
            
            $controller = $getopt->getOption('controller');
            $action     = $getopt->getOption('action');
            $all        = $getopt->getOption('A');
            $verbose    = $getopt->getOption('verbose');

            $front = Zend_Controller_Front::getInstance();
            
            require_once 'Zend/Controller/Request/Http.php';
            $request = new Zend_Controller_Request_Http();
            
            $request->setModuleName($module);
            $request->setControllerName($controller);
            $request->setActionName($action);
            $request->setParam('all', $all);
            $request->setParam('verbose', $verbose);
            $request->setRequestUri("/$module/$controller/$action");

            $front->setRequest($request);

            //$front->setRouter( new Extension_Controller_Router_Cli() );
        } else {

        }
    }



}